<?php

App::uses('AppController', 'Controller');

/**
 * Categories Controller
 *
 * @property Category $Category
 */
class CategoriesController extends AppController {

    public function beforeFilter() {
        parent::beforeFilter();
        $this->Auth->allow('*');
    }
/*
    function index() {
        $categories = $this->Category->generateTreeList(null, null, null, '->');
        debug($categories);
                exit();
        $this->set(compact('categories'));
    }

    function add() {

        if (!empty($this->data)) {
            $this->Category->save($this->data);
            $this->Session->setFlash('A new category has been added');
            $this->redirect(array('action' => 'index'));
        } else {
            $parents[0] = "[ Top ]";
            $categories = $this->Category->generateTreeList(null, null, null, " - ");
            if ($categories) {
                foreach ($categories as $key => $value)
                    $parents[$key] = $value;
            }
            $this->set(compact('parents'));
        }
    }

    function edit($id = null) {
        if (!empty($this->data)) {
            if ($this->Category->save($this->data) == false)
                $this->Session->setFlash('Error saving Category.');
            $this->redirect(array('action' => 'index'));
        } else {
            if ($id == null)
                die("No ID received");
            $this->data = $this->Category->read(null, $id);
            $parents[0] = "[ Top ]";
            $categories = $this->Category->generateTreeList(null, null, null, " - ");
            if ($categories)
                foreach ($categories as $key => $value)
                    $parents[$key] = $value;
            $this->set(compact('parents'));
        }
    }

    function delete($id = null) {
        if ($id == null)
            die("No ID received");
        $this->Category->id = $id;
        if ($this->Category->removeFromTree($id, true) == false)
            $this->Session->setFlash('The Category could not be deleted.');
        $this->Session->setFlash('Category has been deleted.');
        $this->redirect(array('action' => 'index'));
    }
*/
    /**
     * admin_index method
     *
     * @return void
     */
    public function admin_index() {
        $this->Category->recursive = 0;
        $this->set('categories', $this->paginate());
    }

    /**
     * admin_view method
     *
     * @param string $id
     * @return void
     */
    public function admin_view($id = null) {
        $this->Category->id = $id;
        if (!$this->Category->exists()) {
            throw new NotFoundException(__('Invalid category'));
        }
        $this->set('category', $this->Category->read(null, $id));
    }

    /**
     * admin_add method
     *
     * @return void
     */
    public function admin_add() {
        if ($this->request->is('post')) {
            $this->Category->create();
            if ($this->Category->save($this->request->data)) {
                $this->Session->setFlash(__('The category has been saved'));
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('The category could not be saved. Please, try again.'));
            }
        }
        $parentCategories = $this->Category->ParentCategory->find('list');
        $this->set(compact('parentCategories'));
    }

    /**
     * admin_edit method
     *
     * @param string $id
     * @return void
     */
    public function admin_edit($id = null) {
        $this->Category->id = $id;
        if (!$this->Category->exists()) {
            throw new NotFoundException(__('Invalid category'));
        }
        if ($this->request->is('post') || $this->request->is('put')) {
            if ($this->Category->save($this->request->data)) {
                $this->Session->setFlash(__('The category has been saved'));
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('The category could not be saved. Please, try again.'));
            }
        } else {
            $this->request->data = $this->Category->read(null, $id);
        }
        $parentCategories = $this->Category->ParentCategory->find('list');
        $this->set(compact('parentCategories'));
    }

    /**
     * admin_delete method
     *
     * @param string $id
     * @return void
     */
    public function admin_delete($id = null) {
        if (!$this->request->is('post')) {
            throw new MethodNotAllowedException();
        }
        $this->Category->id = $id;
        if (!$this->Category->exists()) {
            throw new NotFoundException(__('Invalid category'));
        }
        if ($this->Category->delete()) {
            $this->Session->setFlash(__('Category deleted'));
            $this->redirect(array('action' => 'index'));
        }
        $this->Session->setFlash(__('Category was not deleted'));
        $this->redirect(array('action' => 'index'));
    }

}
